﻿Imports System.Runtime.CompilerServices
Imports System.Data.Objects
Imports System.IO

Namespace SchoolClick.Extension
    Module Extensor
        <Extension()> _
        Public Sub ExecuteStoreProcedure(context As ObjectContext, storeCommand As String, ParamArray parameters() As Object)
            context.ExecuteStoreCommand(storeCommand, parameters)
        End Sub

        ''' <summary>
        ''' Permite renderizar una vista parcial a un objeto string
        ''' </summary>
        ''' <param name="controller"></param>
        ''' <param name="viewName">Nombre de la vista parcial que se desea renderizar</param>
        ''' <param name="model">El modelo de datos que se va a utilizar para renderizar la vista parcial</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> _
        Public Function ToRenderPartialView(controller As Controller, viewName As String, model As Object) As String
            If String.IsNullOrEmpty(viewName) Then viewName = controller.ControllerContext.RouteData.GetRequiredString("action")
            controller.ViewData.Model = model
            Using sw = New StringWriter()
                Dim viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName)
                Dim viewContext = New ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw)
                viewResult.View.Render(viewContext, sw)

                Return sw.GetStringBuilder().ToString()
            End Using
        End Function
    End Module
End Namespace
